﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
[System.ComponentModel.DefaultEvent("OnQueryClick")]
public partial class Common_QueryControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    #region 查询委托
    private EventHandler OnQueryArg = null;

    [System.ComponentModel.Browsable(true)]
    public event EventHandler OnQueryClick
    {
        add { OnQueryArg += value; }
        remove { OnQueryArg -= value; }
    }
    #endregion

    #region 方法事件
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        if (OnQueryArg != null)
        {
            OnQueryArg(sender, e);
        }
    }
    protected void lbnSwitch_Click(object sender, EventArgs e)
    {
        if (lbnSwitch.CommandArgument == "advance")
        {
            ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
            lbnSwitch.CommandArgument = "simple";
            lbnSwitch.Text = "高级查询";
        }
        else
        {
            ltlAdvance.Text = "";
            lbnSwitch.CommandArgument = "advance";
            lbnSwitch.Text = "简单查询";
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        foreach (Control ctr in PanQuery.Controls)
        {
            if (ctr is TextBox)
            {
                ((TextBox)ctr).Text = "";
            }
            else if (ctr is DropDownList)
            {
                ((DropDownList)ctr).SelectedIndex = 0;
            }
        }
        //清空页面条件状态
        ViewState.Clear();
    }

    #endregion

    #region 属性
    /// <summary>
    /// 通用名
    /// </summary>
    public string ProductName
    {
        get { return HZ.Fun.CheckString(HZ.Supervise.DAL.DAL_Helper.Replace(txtProductName.Text.Trim())); }
        set { txtProductName.Text = value; if (!string.IsNullOrEmpty(value))txtProductName.Enabled = false; }
    }
    /// <summary>
    /// 剂型
    /// </summary>
    public string MedicineModelName
    {
        get { return HZ.Fun.CheckString(HZ.Supervise.DAL.DAL_Helper.Replace(txtMedicineModel.Text.Trim())); }
        set { txtMedicineModel.Text = value; if (!string.IsNullOrEmpty(value))txtMedicineModel.Enabled = false; }
    }
   
    /// <summary>
    /// 配送企业名称
    /// </summary>
    public string CompanyName_PS
    {
        get { return HZ.Fun.CheckString(HZ.Supervise.DAL.DAL_Helper.Replace(txtCompanyName_PS.Text.Trim())); }
        set { txtCompanyName_PS.Text = value; if (!string.IsNullOrEmpty(value))txtCompanyName_PS.Enabled = false; }
    }

    /// <summary>
    /// 配送企业编号
    /// </summary>
    public string CompanyID_PS
    {
        get { return HZ.Fun.CheckString(HZ.Supervise.DAL.DAL_Helper.Replace(txtCompanyID_PS.Value.Trim())); }
        set { txtCompanyID_PS.Value = value; }
    }
    /// <summary>
    /// 生产企业名称
    /// </summary>
    public string CompanyName_SC
    {
        get { return HZ.Fun.CheckString(HZ.Supervise.DAL.DAL_Helper.Replace(txtCompanyName_SC.Text.Trim())); }
        set { txtCompanyName_SC.Text = value; if (!string.IsNullOrEmpty(value))txtCompanyName_SC.Enabled = false; }
    }
    ///// <summary>
    ///// 投标企业名称
    ///// </summary>
    //public string CompanyName_ZB
    //{
    //    get { return HZ.Fun.CheckString(txtCompanyName_ZB.Text.Trim()); }
    //    set { txtCompanyName_ZB.Text = value; if (!string.IsNullOrEmpty(value))txtCompanyName_ZB.Enabled = false; }
    //}
    /// <summary>
    /// 医院名称
    /// </summary>
    public string HospitalName
    {
        get { return HZ.Fun.CheckString(HZ.Supervise.DAL.DAL_Helper.Replace(txtHospitalName.Text.Trim())); }
        set { txtHospitalName.Text = value; if (!string.IsNullOrEmpty(value))txtHospitalName.Enabled = false; }
    }
    /// <summary>
    /// 医院编号
    /// </summary>
    public string HospitalID
    {
        get { return HZ.Fun.CheckString(HZ.Supervise.DAL.DAL_Helper.Replace(txtHospitalID.Value.Trim())); }
        set { txtHospitalID.Value = value; }
    }
    /// <summary>
    /// 药品编号
    /// </summary>
    public string ProcureCatalogID
    {
        get { return HZ.Fun.CheckString(HZ.Supervise.DAL.DAL_Helper.Replace(txtProductID.Value.Trim())); }
        set { txtProductID.Value = value; }
    }

    public string PlanName
    {
        get { return HZ.Fun.CheckString(HZ.Supervise.DAL.DAL_Helper.Replace(txtPlanName.Text.Trim())); }
        set { txtPlanName.Text = value; if (!string.IsNullOrEmpty(value))txtPlanName.Enabled = false; }
    }

    public DateTime? PlanStartTimeFrom
    {
        get { return new HZ.Common.ConvertHelper(HZ.Supervise.DAL.DAL_Helper.Replace(txtStratTime.Text.Trim())).ToDateTime; }
        set { txtStratTime.Text = (value != null) ? value.Value.ToString("yyyy-MM-dd") : ""; if (!string.IsNullOrEmpty(value.ToString()))txtStratTime.Enabled = false; }
    }

    public DateTime? PlanStartTimeTo
    {
        get { return new HZ.Common.ConvertHelper(HZ.Supervise.DAL.DAL_Helper.Replace(txtEndTime.Text.Trim())).ToDateTime; }
        set { txtEndTime.Text = (value != null) ? value.Value.ToString("yyyy-MM-dd") : ""; if (!string.IsNullOrEmpty(value.ToString()))txtEndTime.Enabled = false; }
    }

    public string Pack
    {
        get { return HZ.Fun.CheckString(HZ.Supervise.DAL.DAL_Helper.Replace(txtPack.Text.Trim())); }
        set { txtPack.Text = value; if (!string.IsNullOrEmpty(value))txtPack.Enabled = false; }
    }

    public string Unit
    {
        get { return HZ.Fun.CheckString(HZ.Supervise.DAL.DAL_Helper.Replace(txtUnit.Text.Trim())); }
        set { txtUnit.Text = value; if (!string.IsNullOrEmpty(value))txtUnit.Enabled = false; }
    }
    public string OrderID
    {
        get { return HZ.Fun.CheckString(HZ.Supervise.DAL.DAL_Helper.Replace(txtOrderID.Value.Trim())); }
        set { txtOrderID.Value = value; }
    }

    //拼音码
    public string PinyinCode
    {
        get { return HZ.Fun.CheckString(HZ.Supervise.DAL.DAL_Helper.Replace(txtPinYinCode.Text.Trim())); }
        set { txtPinYinCode.Text = value; }
    }

    //查询方式 模糊、精确
    public bool ProductNameLike
    {
        get { return cbProductNameLike.Checked; }
        set { cbProductNameLike.Checked = value; }
    }

  

    #region DropDownList绑定
    /// <summary>
    /// 药品类别名称
    /// </summary>
    public string SortName
    {
        get
        {
            if (ddlSortName.Text.Trim() == "全部")
            {
                return "";
            }
            else
            {
                return ddlSortName.Text.Trim();
            }
        }
        set
        {
            if (value != null)
                ddlSortName.Text = value.Trim() == "" ? "全部" : value;
        }
    }
    /// <summary>
    /// 质量层次
    /// </summary>
    //public string QualityLevel
    //{
    //    get
    //    {
    //        if (ddlQualityLevel.Text.Trim() == "全部")
    //        {
    //            return "";
    //        }
    //        else
    //        {
    //            return ddlQualityLevel.Text.Trim();
    //        }
    //    }
    //    set
    //    {
    //        if (value != null)
    //            ddlQualityLevel.Text = value.Trim();//== "" ? "全部" : value;
    //    }
    //}
    /// <summary>
    /// 医保类型
    /// </summary>
    //public string InsuranceType
    //{
    //    //这里可以增加适当的转换逻辑
    //    get
    //    {
    //        if (ddlInsuranceType.Text.Trim() == "全部")
    //        {
    //            return "";
    //        }
    //        else
    //        {
    //            return ddlInsuranceType.Text.Trim();
    //        }
    //    }
    //    set
    //    {
    //        if (value != null)
    //        {
    //            for (int i = 0; i < ddlInsuranceType.Items.Count; i++)
    //            {
    //                if (ddlInsuranceType.Items[i].Text == value.Trim())
    //                {
    //                    ddlInsuranceType.SelectedIndex = i;
    //                    break;
    //                }
    //            }
    //        }
    //    }
    //}
    /// <summary>
    /// 医院等级
    /// </summary>
    //public string Levels
    //{
    //    get
    //    {
    //        if (ddlLevels.Text.Trim() == "全部")
    //        {
    //            return "";
    //        }
    //        else
    //        {
    //            return ddlLevels.Text.Trim();
    //        }
    //    }
    //    set
    //    {
    //        if (value != null)
    //        {
    //            for (int i = 0; i < ddlLevels.Items.Count; i++)
    //            {
    //                if (ddlLevels.Items[i].Text == value.Trim())
    //                {
    //                    ddlLevels.SelectedIndex = i;
    //                    break;
    //                }
    //            }
    //        }
    //    }
    //}

    /// <summary>
    /// 采购类型
    /// </summary>
    public string SortSource
    {
        get
        {
            return ddlSortSource.SelectedItem.Value;
        }
        set
        {
            if (value != null)
            {
                ddlSortSource.ClearSelection();
                foreach (ListItem item in ddlSortSource.Items)
                {
                    if (item.Value == value)
                    {
                        item.Selected = true;
                    }
                }
            }
        }
    }



    #endregion DropDownList绑定
    /// <summary>
    /// 排序字段 如果为空 调用页面自己赋值 
    /// </summary>
    public string SortField
    {
        get
        {
            if (ddlFiled.Items.Count > 0)
            {
                return ddlFiled.SelectedItem.Value + " " + ddlDirection.SelectedItem.Value;
            }
            else
            {
                return "";//没有获取Gridview的列的时候返回空，在调用页面 赋值排序字段
            }
        }
    }

    #endregion 属性

    #region 绑定数据源
    private void BindSortName()
    {
        int t = 0;
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        ie.QueryObject = new HZ.Supervise.Model.QueryObject()
        {
            GroupField = GroupByField.SortID,
            SortField = "SortID"
        };
        System.Data.DataTable dt = ie.Execute();

        this.ddlSortName.DataTextField = "SortName";
        this.ddlSortName.DataValueField = "SortName";
        this.ddlSortName.DataSource = dt;
        this.ddlSortName.DataBind();

        ListItem item = new ListItem();
        item.Text = "全部";
        item.Value = "";
        ddlSortName.Items.Insert(0, item);
    }

    //private void BindQualityLevel()
    //{
    //    IList<string> lstQualityLevel = new List<string>();
    //    IBLL_ProcureCatalog ip = HZ.Supervise.BLLFactory.IBLL_ProcureCatalog();
    //    lstQualityLevel = ip.GetQualityLevels();
    //    foreach (string item in lstQualityLevel)
    //    {
    //        ListItem ddlItem = new ListItem();
    //        ddlItem.Text = item;
    //        ddlItem.Value = item;
    //        ddlQualityLevel.Items.Add(ddlItem);
    //    }

    //    ListItem allItem = new ListItem();
    //    allItem.Text = "全部";
    //    allItem.Value = "";
    //    ddlQualityLevel.Items.Insert(0, allItem);
    //}

    /// <summary>
    /// 绑定好排序字段，必须在调用页面绑定完之后执行
    /// </summary>
    /// <param name="gvwInfo"></param>
    public void BindSort(GridView gvwInfo)
    {
        ddlFiled.Items.Clear();
        foreach (var item in gvwInfo.Columns)
        {
            if (item is BoundField)
            {
                if ((item as BoundField).Visible == true)
                {
                    ListItem lstItem = new ListItem();
                    lstItem.Text = (item as BoundField).HeaderText;
                    lstItem.Value = (item as BoundField).DataField;
                    ddlFiled.Items.Add(lstItem);
                }
            }
        }
    }

    /// <summary>
    /// 提前绑定好质量层次,分类等
    /// </summary>
    public void BindDataPre(SuperviseQuery query)
    {
        //绑定质量层次
        //BindQualityLevel();
        //绑定分类名
        BindSortName();

        //BindArea();//初始化地区
        this.AreaControl1.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;

        query.IsUsing = true;

        SetValueFromQuery(query);
        //显示查询条件
        //GetQuery();
        ////绑定地区信息
        //GetArea();
        //绑定数据
        //BindData();
        txtEndTime.Text = DateTime.Now.ToShortDateString();
        txtStratTime.Text = Convert.ToDateTime(HZ.Supervise.DAL.DAL_Helper.Replace(txtEndTime.Text.Trim())).AddMonths(-1).ToShortDateString();
    }

    #endregion 绑定数据源

    #region 条件转换

    /// <summary>
    /// 通过查询的textbox等给查询实体赋值
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    public SuperviseQuery UpdateQueryFromValue(SuperviseQuery query)
    {
        //query = null;
        query.Append = " 1=1 ";
        //生产企业名称
        query.CompanyName_SC = CompanyName_SC;
        //药品编号
        query.ProcureCatalogID = ProcureCatalogID;
        //通用名
        if (ProductNameLike&&!string.IsNullOrEmpty(ProductName))
        {
            query.ProductName = "";
            query.Append = " ProductName='" + ProductName + "'";
        }
        else
        {
            query.ProductName = ProductName;

        }
        //剂型
        query.MedicineModelName = MedicineModelName;

        //单位
        query.Unit = Unit;
        //医院编号
        query.HospitalID = HospitalID;
        //医院名称
        query.HospitalName = HospitalName;
        //商品名
        query.GoodsName = "";

        query.IsUsing = true;
        //包装
        query.Pack = Pack;
        //配送企业名称
        query.CompanyName_PS = CompanyName_PS;
        //配送企业编号
        query.CompanyID_PS = CompanyID_PS;
        //中标企业名称
        //query.CompanyName_TB = CompanyName_ZB;
        //采购单名称
        query.OrderName = PlanName;
        //采购单开始时间 
        //query.StartTimeFrom = PlanStartTimeFrom;
        //query.StartTimeTo = PlanStartTimeTo;

        query.PlanTimeFrom = PlanStartTimeFrom;     //update
        //query.PlanTimeTo = PlanStartTimeTo;
        query.Append += " AND PlanTime<= DATEADD(DAY,1,'" + PlanStartTimeTo + "')";
        //医保类型
        //query.InsuranceType = InsuranceType;
        //医院级别
        //query.Levels = Levels;

        //分类名称
        query.SortName = SortName;

        query.OrderID = OrderID;

        //query.IsPrecious = IsExpensiveDrug;

        //地区信息
        query.Append+= HZ.Supervise.Web.Function.GetAreaIDBySQL(AreaControl1.SelectedAreaID);
        //if (AreaControl1.SelectedAreaID.Split(',').Length > 1)
        //{
        //    query.AreaID = "";
        //    //query.Append = " AreaID IN ('" + AreaID.Replace("'", "").Replace(",", "','") + "')";
        //    query.Append += " AND AreaID IN (" + HZ.Supervise.Web.Function.GetAreaIDBySTR(AreaControl1.SelectedAreaID) +") and  OrderDetailState >0"; // 2010-10-11 yd insert
        //}
        //else
        //{
        //    query.AreaID = HZ.Supervise.Web.Function.GetAreaIDBySTR(AreaControl1.SelectedAreaID);
        //}

        //2010-10-11 yd insert
        if (string.IsNullOrEmpty(query.Append))
        {
            query.Append += " AND OrderDetailState >0";//0代表未提交的采购单，4代表已撤废的采购单
        }
        //else {
        //    query.Append = " and  OrderDetailState not in ('0','4')";//0代表未提交的采购单，4代表已撤废的采购单
        //}

        //拼音码
        query.PinyinCode = PinyinCode;
        //采购类型
        if (!string.IsNullOrEmpty(SortSource))
        {
            query.SortSource = int.Parse(SortSource);
        }
        else
        {
            query.SortSource = null;
        }
        if (!string.IsNullOrEmpty(ddlSortSource.SelectedValue))
        {
            query.SortSource = int.Parse(ddlSortSource.SelectedValue.Trim());
        }
          
        return query;
    }

    /// <summary>
    /// 通过查询实体给textbox等赋值
    /// </summary>
    /// <param name="query"></param>
    private void SetValueFromQuery(SuperviseQuery query)
    {
        CompanyName_SC = query.CompanyName_SC;
        CompanyID_PS = query.CompanyID_PS;
        CompanyName_PS = query.CompanyName_PS;
        //CompanyName_ZB = query.CompanyName_TB;

        HospitalID = query.HospitalID;
        HospitalName = query.HospitalName;
        MedicineModelName = query.MedicineModelName;
        PlanName = query.OrderName;
        Pack = query.Pack;
        ProductName = query.ProductName;
        //PlanStartTimeFrom = query.StartTimeFrom;
        //PlanStartTimeTo = query.StartTimeTo;
        PlanStartTimeFrom = query.PlanTimeFrom;
        PlanStartTimeTo = query.PlanTimeTo;

        Unit = query.Unit;
        ProcureCatalogID = query.ProcureCatalogID;
        OrderID = query.OrderID;
        //**DropDownList
        //InsuranceType = query.InsuranceType;
        //Levels = query.Levels;
        //QualityLevel = query.QualityLevel;
        SortName = query.SortName;

        // IsExpensiveDrug = query.IsPrecious;
        //AreaID = query.AreaID;
        //AreaControl1.InitAreaID = query.AreaID;
        PinyinCode = query.PinyinCode;
        //采购类型
        SortSource = query.SortSource.ToString();

        //if (query.AreaID == null || query.AreaID == "")
        //{
        //    AreaID = "";
        //}
        //else
        //{
        //    if (query.AreaID.Split(',').Length > 1)
        //    {
        //        AreaID = "";//表示全部
        //    }
        //    else
        //    {
        //        AreaID = query.AreaID;
        //    }
        //}

    }
    #endregion



    //#region 选择地区
    ///// <summary>
    ///// 地区编号
    ///// </summary>
    //public string AreaID
    //{
    //    //get
    //    //{
    //    //    if (ddlCity.SelectedValue == "")
    //    //    {
    //    //        //string areaidstr = string.Empty;
    //    //        //for (int i = 1; i < ddlCity.Items.Count; i++)
    //    //        //{
    //    //        //    areaidstr += "," + ddlCity.Items[i].Value;
    //    //        //}
    //    //        //return areaidstr.Substring(1, areaidstr.Length - 1);
    //    //        return CurrentAccount.GetUser().Org.Area.AreaID==Constants.CURRENT_DEPLOY_AREAID?"":CurrentAccount.GetUser().Org.Area.AreaID;
    //    //    }
    //    //    else
    //    //    {
    //    //        return ddlCity.SelectedValue;
    //    //    }
    //    //}

    //    get
    //    {
    //        if (!string.IsNullOrEmpty(ddlCountry.SelectedValue))
    //        {
    //            return ddlCountry.SelectedValue;
    //        }
    //        else
    //        {
    //            return ddlCity.SelectedValue;
    //        }
    //    }

    //    set
    //    {
    //        if (value != null)
    //        {
    //            if (value.Trim() == "")
    //            {
    //                ddlCity.SelectedIndex = 0;
    //            }
    //            else
    //            {
    //                string[] areaArray = value.ToString().Split(',');

    //                if (areaArray.Length > 1)
    //                {
    //                    ddlCity.SelectedIndex = 0;//多个地区的时候，显示全部
    //                }
    //                else
    //                {
    //                    for (int i = 0; i < ddlCity.Items.Count; i++)
    //                    {
    //                        if (ddlCity.Items[i].Value == value.Trim())
    //                        {
    //                            ddlCity.SelectedIndex = i;
    //                            break;
    //                        }
    //                    }
    //                }
    //            }
    //        }
    //    }
    //}

    //public void BindArea()
    //{
    //    IBLL_Area ia = new HZ.Supervise.BLL.BLL_Area();
    //    IList<Area> lstArea = new List<Area>();
    //    string userAreaId = CurrentAccount.GetUser().Org.Area.AreaID;
    //    //判断用户所属地区，如果用户是省级的 则可以列出全部区域
    //    if (userAreaId == CurrentAccount.GetCurrentDeployArea().AreaID)
    //    {
    //        lstArea = ia.GetSonAreas(userAreaId);
    //        foreach (Area area in lstArea)
    //        {
    //            if (area.AreaName == "省直属")
    //            {
    //                ListItem item = new ListItem();
    //                item.Text = area.AreaName;
    //                item.Value = area.AreaID;
    //                ddlCity.Items.Insert(0, item);
    //            }
    //            else
    //            {
    //                ListItem ddlitem = new ListItem();
    //                ddlitem.Text = area.AreaName;
    //                ddlitem.Value = area.AreaID;
    //                ddlCity.Items.Add(ddlitem);
    //            }
    //        }

    //        //ListItem a = new ListItem();
    //        //a.Text = "省直属";
    //        //a.Value = userAreaId;
    //        //ddlCity.Items.Insert(0, a);

    //        ListItem b = new ListItem();
    //        b.Text = "全部";
    //        b.Value = "";
    //        ddlCity.Items.Insert(0, b);
    //    }
    //    else
    //    {
    //        lstArea = ia.GetAreasByAreaID(userAreaId);
    //        foreach (Area area in lstArea)
    //        {
    //            ListItem ddlitem = new ListItem();
    //            ddlitem.Text = area.AreaName;
    //            ddlitem.Value = area.AreaID;
    //            ddlCity.Items.Add(ddlitem);
    //        }
    //    }
    //    //绑定数据源
    //    //如果有当前省的编号，则增加省直属 
    //    //同时地区获取值的要设置 

    //    //绑定区县
    //    ddlCity_SelectedIndexChanged(null, null);

    //}

    ///// <summary>
    ///// 更换选择城市后 触发
    ///// </summary>
    ///// <param name="sender"></param>
    ///// <param name="e"></param>
    //protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    string strCity = (this.ddlCity.SelectedItem != null) ? this.ddlCity.SelectedItem.Value : "";

    //    this.ddlCountry.Items.Clear();

    //    IBLL_Area ia = new HZ.Supervise.BLL.BLL_Area();
    //    IList<Area> lstArea = new List<Area>();
    //    if (!string.IsNullOrEmpty(strCity))
    //    {
    //        lstArea = ia.GetSonAreas(strCity);

    //        foreach (Area area in lstArea)
    //        {
    //            ListItem ddlitem = new ListItem();
    //            ddlitem.Text = area.AreaName;
    //            ddlitem.Value = area.AreaID;
    //            this.ddlCountry.Items.Add(ddlitem);
    //        }
    //    }

    //    ListItem b = new ListItem();
    //    b.Text = "所在区县";
    //    b.Value = "";
    //    ddlCountry.Items.Insert(0, b);
    //}
    //#endregion




}
